package cn.ronghuanet.org.service.impl;

import cn.ronghuanet.base.query.BaseQuery;
import cn.ronghuanet.base.service.impl.BaseServiceImpl;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.mapper.DepartmentMapper;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;

    @Transactional
    @Override
    public void update(Department department) {
        if (Objects.nonNull(department.getParent() )
                &&  Objects.nonNull(department.getParent().getId())){
            department.setPath(department.getPath()+"/"+department.getId());
        }else{
            department.setPath("/"+department.getId());
        }

        mapper.update(department);
    }


    @Transactional
    @Override
    public void insert(Department department) {
        mapper.insert(department);
        department.setPath(department.getPath()+"/"+department.getId());
        mapper.update(department);
    }


    @Override
    public List<Department> queryDeptTree() {
        return mapper.queryDeptTree();
    }
}
